4D - Mysterious Present - CodeForces Solution


dp sortings *1700

Please click on ads to support us..

Python Code:

import sys






input = sys.stdin.readline
n, width, height = map(int, input().split())
arr = []
for i in range(n):
    w, h = map(int, input().split())
    if w > width and h > height:
        arr.append((w, h, i+1))

F = [1]*len(arr)
parent = [-1]*len(arr)
arr.sort(key=lambda x:x[0], reverse=True)

for i in range(len(arr)):
    for j in range(i):
        if arr[j][0] > arr[i][0] and arr[j][1] > arr[i][1] and F[j]+1 > F[i]:
            F[i] = F[j]+1
            parent[i] = j
ind = -1
m = 0
for i in range(len(arr)):
    if F[i] > m:
        m = F[i]
        ind = i

ans = []
print(m)
while ind != -1:
    ans.append(arr[ind][2])
    ind = parent[ind]
if len(ans):
    print(*(ans))

C++ Code:

// Hydro submission #6410a2f4cff12ea87106af21@1678811892987
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 5005;

int f[N],g[N];
int n,x,y,ans;

struct node{
    int id;
    int x;
    int y;
    bool operator<(const node &aa) const{
        if(y == aa.y){
            return y < aa.y;
        }
        return x < aa.x;
    }
}a[N];

void Index(int ix){
    if(ix == -1)
    return;
    Index(g[ix]);
    cout<<a[ix].id<<" ";
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin>>n>>x>>y;
    for(int i = 0;i < n;i++){
        cin>>a[i].x>>a[i].y;
        a[i].id = i+1;
    }
    sort(a,a+n);
    memset(f,0,sizeof(f));
    memset(g,-1,sizeof(g));

    for(int i = 0;i < n;i++){
        if(a[i].x > x && a[i].y > y){
            f[i] = 1;
            for(int j = 0;j < i;j++){
                if(a[j].x < a[i].x &&a[j].y < a[i].y && f[j] >= f[i]){
                    f[i] = f[j]+1;
                    g[i] = j;
                }
            }
            if(f[ans] < f[i]){
                ans = i;
            }
        }
    }
    cout<<f[ans]<<endl;
    if(f[ans]){
        Index(ans);
    }

    return 0;
}


Comments

Submit
0 Comments
More Questions

1279A - New Year Garland
1279B - Verse For Santa
202A - LLPS
978A - Remove Duplicates
1304A - Two Rabbits
225A - Dice Tower
1660D - Maximum Product Strikes Back
1513A - Array and Peaks
1251B - Binary Palindromes
768B - Code For 1
363B - Fence
991B - Getting an A
246A - Buggy Sorting
884A - Book Reading
1180A - Alex and a Rhombus
445A - DZY Loves Chessboard
1372A - Omkar and Completion
159D - Palindrome pairs
981B - Businessmen Problems
1668A - Direction Change
1667B - Optimal Partition
1668B - Social Distance
88B - Keyboard
580B - Kefa and Company
960A - Check the string
1220A - Cards
897A - Scarborough Fair
1433B - Yet Another Bookshelf
1283B - Candies Division
1451B - Non-Substring Subsequence